﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BTv2.Controls;
using BTv2.UserControls;

namespace BTv2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        private static MainWindow _mwinstance;

        public MainWindow()
        {
            InitializeComponent();
            _mwinstance = this;

        }
        public static MainWindow GetInstance()
        {
            return _mwinstance;
        }

        public void AttachSettingClick()
        {
            SettingsButton.Click += SettingsClicked;
        }

        private void SettingsClicked(object sender, RoutedEventArgs e)
        {
            new LoginWindow().Show();
            SettingsButton.Click -= SettingsClicked;
        }

        private async void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            mainWindowTransitionalContetntControl.Content = new MainWindowDefaultState(this);
            bool industrialPCStatus = await Task.Run(() => { return IndustrialPCController.GetInstance().IsAlive(); });
            if (!industrialPCStatus)
            {
                MessageBox.Show("Error 469. Problem connecting with the Industrial PC.");
                this.Close();
            }
            bool munitsfetched = await Task.Run(() => { return SensorsController.GetInstance().PreloadMericUnits(); });
            bool stypesfetched = await Task.Run(() => { return SensorsController.GetInstance().PreloadSensorTypes(); });
            bool sensorsFetched = await Task.Run(() => { return SensorsController.GetInstance().PreloadSensors(); });
            bool sensorGroupsFetched = await Task.Run(() => { return SensorsController.GetInstance().PreloadSensorGroups(); });
            bool usersFetched = await Task.Run(() => { return UsersController.GetInstance().PreloadUserList(); });
            bool mailListsFetched = await Task.Run(() => { return UsersController.GetInstance().PreloadMailingLists(); });
            bool rulesfetched = await Task.Run(() => { return RulesController.GetInstance().PreloadRules(); });
            bool wheaterFetched = await Task.Run(() => { return WeatherController.GetInstance().PreloadWheater(); });

        }

        public void ShowGraph(object sender, RoutedEventArgs args)
        {
            var GraphTile = new GraphTile();
            mainWindowTransitionalContetntControl.Content = GraphTile;
        }

        public void ShowWheater(object sender, RoutedEventArgs args)
        {
            var WheaterTile = new WheaterTile();
            mainWindowTransitionalContetntControl.Content = WheaterTile;
        }


        private void MainWindowHomeButton_Click(object sender, RoutedEventArgs e)
        {
            if( !(mainWindowTransitionalContetntControl.Content is MainWindowDefaultState)){
            mainWindowTransitionalContetntControl.Content = new MainWindowDefaultState(this);
            }
        }
    }
}
